﻿using System;
using System.ComponentModel.Composition.Hosting;
using System.Windows;
using MEF_Test.Bricks.Wrappers;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Unity;
using WPFMediaKit.DirectShow.Controls;

namespace MEF_Test.Bricks.Behaviors
{
    public class MydraMediaUriElementPlayerControlsProxyBehavior
    {
        public static bool GetIsEnabled(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsEnabledProperty);
        }

        public static void SetIsEnabled(DependencyObject obj, bool value)
        {
            obj.SetValue(IsEnabledProperty, value);
        }

        // Using a DependencyProperty as the backing store for IsEnabled.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsEnabledProperty =
            DependencyProperty.RegisterAttached("IsEnabled", typeof(bool), typeof(MediaUriElement), new UIPropertyMetadata(false, OnIsEnabledChanged));

        private static void OnIsEnabledChanged(DependencyObject control, DependencyPropertyChangedEventArgs e)
        {
            var element = control as MydraMediaUriElement;
            if (element == null)
            {
                throw new ArgumentException("The attached control is not a valid MydraMediaUriElement");
            }
            
            // We use an IoC because since it is a static method, we cannot use MEF's ImportingConstructor to leverage dependency injection. we then have to resort to using IoC : Couldn't find any other alternative.            

            // IoC static class to get the Unity container ( as of november 2010 ), resolving the MEF container, and getting the exported MefEventAggregator value.
            var eventAggregator = IoC.Current.Resolve<CompositionContainer>().GetExportedValue<IEventAggregator>();


            var mediaPauseRequestedAggregatedEvent = eventAggregator.GetEvent<CompositePresentationEvent<MediaPauseRequestedAggregatedEvent>>();
            var callback = new Action<MediaPauseRequestedAggregatedEvent>(ea => MediaPlaybackStatusChanged(element, ea));
            if ((bool)e.NewValue)
            {
                mediaPauseRequestedAggregatedEvent.Subscribe(callback, true);
            }
            else
            {
                mediaPauseRequestedAggregatedEvent.Unsubscribe(callback);
            }
            
        }

        private static void MediaPlaybackStatusChanged(MydraMediaUriElement targetElement, MediaPauseRequestedAggregatedEvent eventAgs)
        {
            switch (eventAgs.Status)
            {
                case PlaybackStatus.Play:
                    targetElement.Play();
                    break;
                case PlaybackStatus.Pause:
                    targetElement.Pause();
                    break;
                case PlaybackStatus.Stop:
                    targetElement.Stop();
                    targetElement.MediaPosition = 0;
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
    }
}
